#include "Grabber.h"

Grabber::Grabber() 
{
	memset(keyDown, 0, sizeof(keyDown));

	up     = boost::numeric::ublas::vector<float> (3);
	eye    = boost::numeric::ublas::vector<float> (3);
	center = boost::numeric::ublas::vector<float> (3);

	up (0)     =  0.0;  up (1)     =  1.0;  up (2)	   =  0.0;
	eye (0)    =  0.0;  eye (1)    =  0.0;  eye (2)    =  0.0;
	center (0) =  0.0;  center (1) =  0.0;  center (2) = -1.0;

	reset();
}

Grabber::~Grabber() 
{
	//delete keyDown;
}

void Grabber::update(){
	float tickDist = 0.1;

	// check if key is down and modify rot* and tr* accordingly
	if(keyDown[(unsigned char)('w')]){
		eye(2) -= 0.01;
		center(2) -= 0.01;
	}
	if(keyDown[(unsigned char)('s')]){
		eye(2) += 0.01;
		center(2) += 0.01;
	}
	if(keyDown['a']){
		eye(0) -= 0.01;
		center(2) -= 0.01;
	}
	if(keyDown['d']){
		eye(0) += 0.01;
		center(2) += 0.01;
	}
	if(keyDown[';']){
		printStatus();
	}
	if(keyDown[0x1B]){
		exit(0);
	}
	glutPostRedisplay();
}

void Grabber::transform() 
{
	update();
	gluLookAt(eye(0), eye(1), eye(2), 
		      center(0), center(1), center(2),
			  up(0), up(1), up(2));
}

void Grabber::handleKeyDown(unsigned char key, int x, int y) 
{
	//std::cout << ".";
	keyDown[key]=1;
	glutPostRedisplay();
}

void Grabber::handleKeyUp(unsigned char key, int x, int y)
{
	keyDown[key] = 0;
	glutPostRedisplay();
}

void Grabber::handleMouse(int button, int state,int x, int y) 
{
	// TODO: Should also do something usefull with the mouse clicks
	lastX = x; 
	lastY = y;
	state = state; 
	button = button;
}

void Grabber::handleMouseMotion(int x, int y)
{
	int diffX = lastX - x;
	int diffY = lastY - y;
	lastX = x; 
	lastY = y;
	rotX += ((float)diffY)/10.0;
	rotY += ((float)diffX)/10.0;
}

std::string Grabber::toString() 
{
	return "";
}

void Grabber::reset() 
{
	trX  = 0;  trY  = 0;  trZ  = 0;
	rotX = 0;  rotY = 0;  rotZ = 0;
}


void Grabber::printStatus(){
	std::cout << "center = " << center << " | eye = " << eye << " | up = " << up << "\n";
}

void Grabber::rotate(){

	return;
}

void Grabber::translate(){

}